package topin.facecat.FaceCat

import android.Manifest
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import facecat.topin.sock.FCClientSocket
import topin.facecat.FaceCat.Android.FCUIView
import topin.facecat.FaceCat.UI.MainFrame


/*
* 主视图
*/
class MainActivity : AppCompatActivity() {
    /**
     * 创建视图
     * @param savedInstanceState
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (m_mainActivity != null && m_mainActivity !== this) {
            finish()
            return
        }
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        m_mainActivity = this
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) == PackageManager.PERMISSION_DENIED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                1
            )
        }
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ) == PackageManager.PERMISSION_DENIED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
                1
            )
        }
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.CAMERA
            ) == PackageManager.PERMISSION_DENIED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.CAMERA),
                1
            )
        }
        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.INTERNET
            ) == PackageManager.PERMISSION_DENIED
        ) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.INTERNET),
                1
            )
        }
        FCClientSocket.noThread = false
        val view = FCUIView(baseContext)
        view.setAllowScroll(false)
        view.onLoad()
        val mainFrame = MainFrame()
        mainFrame.native = view.native
        mainFrame.loadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
                "<html xmlns=\"facecat\">\n" +
                "    <body>\n" +
                "        <div bordercolor=\"none\" name=\"divInner\" dock=\"fill\" >\n" +
                "            <div type=\"tab\" dock=\"fill\" selectedindex=\"0\" backcolor=\"none\" bordercolor=\"none\"\n" +
                "                name=\"tabFunc\">\n" +
                "                <div type=\"tabpage\" text=\"列表\" name=\"divMain\" backcolor=\"none\">\n" +
                "                    <div type=\"custom\" cid=\"securitylist\" name=\"gridStocks\" headerheight=\"30\" dock=\"fill\"\n" +
                "                        gridlinecolor=\"none\" bordercolor=\"none\" showvscrollbar=\"true\" showhscrollbar=\"true\"\n" +
                "                        allowpreviewsevent=\"true\" allowdragscroll=\"true\">\n" +
                "                        <tr>\n" +
                "                            <th name=\"colP0\" text=\"序\" width=\"40\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" />\n" +
                "                            <th name=\"colP1\" text=\"代码\" width=\"70\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" />\n" +
                "                            <th name=\"colP2\" text=\"名称\" width=\"70\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" />\n" +
                "                            <th name=\"colP3\" text=\"现价\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP4\" text=\"涨幅\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP5\" text=\"涨跌\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP9\" text=\"总量\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP10\" text=\"总额\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP11\" text=\"量比\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP12\" text=\"PE动\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP13\" text=\"振幅\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP14\" text=\"换手率\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP15\" text=\"总市值\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP16\" text=\"流值\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP17\" text=\"行业分类板块\" width=\"80\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"center\"/>\n" +
                "                            <th name=\"colP18\" text=\"涨停价\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP19\" text=\"跌停价\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP20\" text=\"金比\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP21\" text=\"涨跌比\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP22\" text=\"涨速\" width=\"60\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP23\" text=\"净资产收益率\" width=\"100\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"-200000000193\"\n" +
                "                                bordercolor=\"-200000000108\" textcolor=\"-200000000100\" cellalign=\"right\"/>\n" +
                "                            <th name=\"colP24\" text=\"TAG\" width=\"0\" allowdrag=\"true\" allowresize=\"true\" backcolor=\"rgb(100,100,100)\"\n" +
                "                                bordercolor=\"none\" textcolor=\"rgb(210,210,210)\" cellalign=\"right\" visible=\"false\"/>\n" +
                "                        </tr>\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "                <div type=\"tabpage\" text=\"K线\" name=\"divKLine\" backcolor=\"none\">\n" +
                "                    <div type=\"splitlayout\" layoutstyle=\"toptobottom\" backcolor=\"none\" bordercolor=\"none\"\n" +
                "                        dock=\"fill\" size=\"400,400\" candragsplitter=\"true\" splitmode=\"percentsize\" splitterposition=\"200,1\">\n" +
                "                        <div type=\"splitlayout\" layoutstyle=\"righttoleft\" backcolor=\"none\" bordercolor=\"none\"\n" +
                "                            dock=\"fill\" size=\"400,400\" candragsplitter=\"true\" splitterposition=\"250,1\">\n" +
                "                            <div type=\"custom\" cid=\"latestdiv\" name=\"divLatest\" dock=\"fill\"/>\n" +
                "                            <div type=\"custom\" cid=\"mychart\" name=\"mainChart1\" cycle=\"0\" dock=\"fill\"/>\n" +
                "                        </div>\n" +
                "                        <div type=\"custom\" cid=\"mychart\" name=\"mainChart3\" cycle=\"1440\" dock=\"fill\"/>\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "                <div type=\"tabpage\" text=\"多K线\" name=\"divMulti\" backcolor=\"none\">\n" +
                "                    <div type=\"splitlayout\" layoutstyle=\"lefttoright\" backcolor=\"none\" bordercolor=\"none\"\n" +
                "                        dock=\"fill\" size=\"400,400\" candragsplitter=\"true\" splitmode=\"percentsize\" splitterposition=\"133,1\">\n" +
                "                        <div type=\"splitlayout\" layoutstyle=\"toptobottom\" backcolor=\"none\" bordercolor=\"none\"\n" +
                "                            dock=\"fill\" size=\"400,400\" candragsplitter=\"true\" splitmode=\"percentsize\" splitterposition=\"267,1\">\n" +
                "                            <div type=\"splitlayout\" layoutstyle=\"toptobottom\" backcolor=\"none\" bordercolor=\"none\"\n" +
                "                                dock=\"fill\" size=\"400,400\" candragsplitter=\"true\" splitmode=\"percentsize\" splitterposition=\"200,1\">\n" +
                "                                <div type=\"custom\" cid=\"mychart\" name=\"chart1\" cycle=\"1\" nativerefresh=\"true\" candledivpercent=\"1\"\n" +
                "                                    voldivpercent=\"0\" />\n" +
                "                                <div type=\"custom\" cid=\"mychart\" name=\"chart2\" cycle=\"5\" nativerefresh=\"true\" candledivpercent=\"1\"\n" +
                "                                    voldivpercent=\"0\" />\n" +
                "                            </div>\n" +
                "                            <div type=\"custom\" cid=\"mychart\" name=\"chart3\" cycle=\"10\" nativerefresh=\"true\" candledivpercent=\"1\"\n" +
                "                                voldivpercent=\"0\" />\n" +
                "                        </div>\n" +
                "                        <div type=\"splitlayout\" layoutstyle=\"lefttoright\" backcolor=\"none\" bordercolor=\"none\"\n" +
                "                            dock=\"fill\" size=\"400,400\" candragsplitter=\"true\" splitmode=\"percentsize\" splitterposition=\"200,1\">\n" +
                "                            <div type=\"splitlayout\" layoutstyle=\"toptobottom\" backcolor=\"none\" bordercolor=\"none\"\n" +
                "                                dock=\"fill\" size=\"400,400\" candragsplitter=\"true\" splitmode=\"percentsize\" splitterposition=\"267,1\">\n" +
                "                                <div type=\"splitlayout\" layoutstyle=\"toptobottom\" backcolor=\"none\" bordercolor=\"none\"\n" +
                "                                    dock=\"fill\" size=\"400,400\" candragsplitter=\"true\" splitmode=\"percentsize\" splitterposition=\"200,1\">\n" +
                "                                    <div type=\"custom\" cid=\"mychart\" name=\"chart4\" cycle=\"15\" nativerefresh=\"true\" candledivpercent=\"1\"\n" +
                "                                        voldivpercent=\"0\" />\n" +
                "                                    <div type=\"custom\" cid=\"mychart\" name=\"chart5\" cycle=\"20\" nativerefresh=\"true\" candledivpercent=\"1\"\n" +
                "                                        voldivpercent=\"0\" />\n" +
                "                                </div>\n" +
                "                                <div type=\"custom\" cid=\"mychart\" name=\"chart6\" cycle=\"30\" nativerefresh=\"true\" candledivpercent=\"1\"\n" +
                "                                    voldivpercent=\"0\" />\n" +
                "                            </div>\n" +
                "                            <div type=\"splitlayout\" layoutstyle=\"toptobottom\" backcolor=\"none\" bordercolor=\"none\"\n" +
                "                                dock=\"fill\" size=\"400,400\" candragsplitter=\"true\" splitmode=\"percentsize\" splitterposition=\"267,1\">\n" +
                "                                <div type=\"splitlayout\" layoutstyle=\"toptobottom\" backcolor=\"none\" bordercolor=\"none\"\n" +
                "                                    dock=\"fill\" size=\"400,400\" candragsplitter=\"true\" splitmode=\"percentsize\" splitterposition=\"200,1\">\n" +
                "                                    <div type=\"custom\" cid=\"mychart\" name=\"chart7\" cycle=\"1440\" nativerefresh=\"true\"\n" +
                "                                        candledivpercent=\"1\" voldivpercent=\"0\" />\n" +
                "                                    <div type=\"custom\" cid=\"mychart\" name=\"chart8\" cycle=\"10080\" nativerefresh=\"true\"\n" +
                "                                        candledivpercent=\"1\" voldivpercent=\"0\" />\n" +
                "                                </div>\n" +
                "                                <div type=\"custom\" cid=\"mychart\" name=\"chart9\" cycle=\"43200\" nativerefresh=\"true\"\n" +
                "                                    candledivpercent=\"1\" voldivpercent=\"0\" />\n" +
                "                            </div>\n" +
                "                        </div>\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "            </div>\n" +
                "        </div>\n" +
                "    </body>\n" +
                "</html>\n")
        //view.setLoadUI(m_chatMainFrame);
        setContentView(view)
        mainFrame.native.update()
        mainFrame.native.invalidate()
    }

    /**
     * 回退
     */
    override fun onBackPressed() {
        moveTaskToBack(true)
    }

    /**
     * 配置改变
     * @param newConfig
     */
    override fun onConfigurationChanged(newConfig: Configuration) {
        // TODO Auto-generated method stub
        try {
            super.onConfigurationChanged(newConfig)
            if (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            } else if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    companion object {
        /*
        * 主视图静态对象
         */
        var m_mainActivity: MainActivity? = null
    }
}